package org.test.controller;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.test.service.AdminService;
import org.test.service.BookService;
import org.test.service.ShoppingCartService;
import org.test.vo.Admin;
import org.test.vo.Book;
import org.test.vo.ShoppingCart;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller  //注解控制器
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    //注入服务层对象
    @Autowired
    ShoppingCartService  shoppingCartService;
    @Autowired
    AdminService adminService;
    @Autowired
    BookService bookService;

    //添加图书到购物车
    @RequestMapping("/addBookToShoppingCart")
    public  String addBookToShoppingCart(Model model, int bookid, String bookname, HttpSession session){
        String username = (String) session.getAttribute("username");
        Admin admin=adminService.getAdminByUname(username);
        //判断购物车是否有该图书记录
        ShoppingCart shoppingCartBook=shoppingCartService.selectShoppingByBook(admin.getId(),bookid);
        //有记录，booknum自增1
       if (shoppingCartBook !=null){
           int booknum=shoppingCartBook.getBooknum()+1;
           int addbooknum = shoppingCartService.addBooknumToShoppingCart(shoppingCartBook.getCartid(),booknum);
           if (addbooknum !=0){
               model.addAttribute("message","购物车自增1成功");
               return "public/message";
           }
           else {
               model.addAttribute("message","购物车自增1失败");
               return "public/message";
           }

       }
       else{
           //无记录，新增记录
           int addToshoppingCart=shoppingCartService.addBookToShoppingCart(admin.getId(),bookid,bookname,1);
           if (addToshoppingCart != 0){
               model.addAttribute("message","恭喜你添加成功");
               return "public/message";
           }
           else {
               model.addAttribute("message","失败");
               return "public/message";
           }

       }
    }

    //查询用户购物车的全部信息
    @RequestMapping("/shoppingCartAll")
    public String shoppingCartAll(Model model,String username){
        Admin admin=adminService.getAdminByUname(username);
        int id=admin.getId();
        //获得购物车列表
        List<ShoppingCart> shoppingCartList =shoppingCartService.selectShoppingCartAll(id);
        model.addAttribute("shoppingCartList",shoppingCartList);
        //新建list
        List<Book> bookList= new ArrayList<Book>();
        //循环购物车列表
        for (int i=0; i<shoppingCartList.size();i++) {
            //获得单条购物车信息
            ShoppingCart j = shoppingCartList.get(i);
            //根据购物车单条信息的图书编号获得图书信息
            Book shoppingCartBook=bookService.getBookByBookid(j.getBookid());
            bookList.add(shoppingCartBook);
        }

        model.addAttribute("shoppingCartBook",bookList);
        return "user/user-shoppingCart";

    }
    //删除购物车
    @RequestMapping("/deleteShoppingCart")
    public String deleteOrder(Model model, int cartid){
        shoppingCartService.deleteShoppingCart(cartid);
        model.addAttribute("message","恭喜删除购物车成功");
        return "public/message";
    }


}
